
package com.yimixian.app.wxapi;

import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.yimixian.app.R;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.WebViewActivity;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.net.HttpApi;
import com.yimixian.app.util.SharedPreferencesHelper;
import com.yimixian.app.util.UiUtils;
import com.yimixian.app.util.WeChatUtils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI mIWXAPI;
    private DataManager mDataManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(null);

        mIWXAPI = WXAPIFactory.createWXAPI(this, Constants.WECHAT_APP_ID, false);
        mIWXAPI.registerApp(Constants.WECHAT_APP_ID);
        mDataManager = DataManager.getInstance();
        mIWXAPI.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        setIntent(intent);
        mIWXAPI.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp baseResp) {
        int shareResult = WeChatUtils.WE_CHAT_SHARE_FAIL;

        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                shareResult = WeChatUtils.WE_CHAT_SHARE_SUCCESS;
                if(baseResp instanceof SendAuth.Resp){
                    final SendAuth.Resp resp = (SendAuth.Resp) baseResp;
                    Map<String, String> map = new HashMap<>();
                    map.put("code", resp.code);
                    HttpApi.startPostRequest("/v3/login_by_wechat", map, new HttpApi.RequestCallBack() {
                        @Override
                        public void requestSuccess(String result) {
                            sendBroadcast(new Intent().putExtra("data", result).setAction("com.ymx.data"));
                        }

                        @Override
                        public void requestError() {
                        }

                        @Override
                        public void alwaysDo() {
                            finish();
                            overridePendingTransition(0, 0);
                        }
                    });
                    return;
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                sendBroadcast(new Intent().putExtra("data", "cancel").setAction("com.ymx.data"));
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
            default:
                shareResult = WeChatUtils.WE_CHAT_SHARE_FAIL;
                break;
        }
        mDataManager.put(WebViewActivity.EXTRA_SHARE_RESULT, shareResult);
        finish();
    }
}
